package com.sqx.modules.taking.controller;

import com.sqx.common.utils.Result;
import com.sqx.modules.orders.dao.OrdersDao;
import com.sqx.modules.orders.service.OrdersService;
import com.sqx.modules.taking.dao.OrderTakingDao;
import com.sqx.modules.taking.entity.OrderTaking;
import com.sqx.modules.taking.service.OrderTakingService;
import com.sqx.modules.task.dao.HelpTakeDao;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/orderTaking")
public class OrderTakingController {

    @Autowired
    private OrderTakingService orderTakingService;
    @Autowired
    private OrdersDao ordersDao;
    @Autowired
    private OrderTakingDao orderTakingDao;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private HelpTakeDao helpTakeDao;

    /**
     * 发布接单
     */
    @ApiOperation("发布接单")
    @PostMapping("/insertOrderTaking")
    public Result insertOrderTaking(@RequestBody OrderTaking orderTaking) {
        return orderTakingService.insertOrderTaking(orderTaking,1);
    }

    /**
     * 删除订单
     */
    @ApiOperation("删除接单")
    @GetMapping("/deleteOrderTaking")
    public Result deleteOrderTaking(Long id) {
        return orderTakingService.deleteOrderTaking(id);
    }

    /**
     * 查询所有接单
     */
    @ApiOperation("查询所有接单")
    @GetMapping("/queryAllOrderTaking")
    public Result queryAllOrderTaking(@ApiParam("页") Integer page, @ApiParam("条") Integer limit, String city, String gameId,
                                      Long status, String userName, Long userId, Integer classify,Integer isRecommend,String myLevel) {
        return orderTakingService.queryAllOrderTaking(page, limit, city, gameId, status, userName, userId, classify,isRecommend,myLevel);
    }

    /**
     * 审核接单
     */
    @ApiOperation("审核接单")
    @GetMapping("/auditorOrderTaking")
    public Result auditorOrderTaking(Long id, Integer status, String content) {
        return orderTakingService.auditorOrderTaking(id, status, content);
    }

    /**
     * 修改
     */
    @ApiOperation("修改接单")
    @PostMapping("/updateTakingOrder")
    public Result updateTakingOrder(@RequestBody OrderTaking orderTaking) {
        return orderTakingService.updateTakingOrder(orderTaking,1);
    }

    @ApiOperation("修改接单")
    @PostMapping("/updateTakingOrders")
    public Result updateTakingOrders(@RequestBody OrderTaking orderTaking) {
        return orderTakingService.updateTakingOrders(orderTaking,1);
    }

    @PostMapping("/updateOrderTakingStatus/{id}")
    @ApiOperation("修改状态")
    public Result updateOrderTakingStatus(@PathVariable Long id) {
        OrderTaking byId = orderTakingService.getById(id);
        if (byId.getStatus() == 0) {
            byId.setStatus(2);
        } else {
            byId.setStatus(0);
        }
        orderTakingService.updateById(byId);
        return Result.success();
    }

    @PostMapping("/updateOrderTakingRecommend/{id}")
    @ApiOperation("修改状态")
    public Result updateOrderTakingRecommend(@PathVariable Long id) {
        OrderTaking byId = orderTakingService.getById(id);
        if ("0".equals(byId.getIsRecommend())) {
            byId.setIsRecommend("1");
        } else {
            byId.setIsRecommend("0");
        }
        orderTakingService.updateById(byId);
        return Result.success();
    }

    @PostMapping("/updateOrderTakingSort/{id}")
    @ApiOperation("修改排序")
    public Result updateOrderTakingSort(@PathVariable Long id,Integer sort) {
        OrderTaking byId = orderTakingService.getById(id);
        byId.setSort(sort);
        orderTakingService.updateById(byId);
        return Result.success();
    }

    @ApiOperation("任务分析")
    @GetMapping("/taskAnalysis")
    public Result taskAnalysis(String time, Integer flag) {
        Integer sendOrderCount = ordersDao.countOrdersByCreateTime(time, flag);//
        BigDecimal sendOrderSum = ordersDao.sumOrdersByCreateTime(time, flag, null);
        Integer takeOrderCount = orderTakingDao.countGoodsByCreateTime(time, flag);
        BigDecimal takeOrderSum = orderTakingDao.sumGoodsByCreateTime(time, flag);

        Integer helperOrderAllCount = helpTakeDao.selectHelperOrderCount(time, flag, null);
        Integer helperOrderCompleteCount = helpTakeDao.selectHelperOrderCount(time, flag, 4);
        BigDecimal helperOrderAllMoney= helpTakeDao.selectHelperOrderMoney(time, flag, null);
        BigDecimal helperOrderCompleteMoney= helpTakeDao.selectHelperOrderMoney(time, flag, 4);
        Map<String, Object> map = new HashMap<>();
        map.put("takeOrderSum", takeOrderSum.setScale(2,BigDecimal.ROUND_DOWN));//发单总金额
        map.put("sendOrderSum", sendOrderSum.setScale(2,BigDecimal.ROUND_DOWN));//接单总金额
        map.put("takeOrderCount", takeOrderCount);//发单数量
        map.put("sendOrderCount", sendOrderCount);//接单数量
        //万能任务总订单数
        map.put("helperOrderAllCount",helperOrderAllCount);
        //万能任务完成总数
        map.put("helperOrderCompleteCount",helperOrderCompleteCount);
        //万能任务总金额
        map.put("helperOrderAllMoney",helperOrderAllMoney.setScale(2,BigDecimal.ROUND_DOWN));
        //万能任务完成总金额
        map.put("helperOrderCompleteMoney",helperOrderCompleteMoney.setScale(2,BigDecimal.ROUND_DOWN));
        return Result.success().put("data", map);
    }

    @ApiOperation("接单收入分析")
    @GetMapping("/incomeAnalysis")
    public Result incomeAnalysis(String time, Integer flag, int page, int limit) {
        return Result.success().put("data", ordersService.incomeAnalysisOrders(page, limit, time, flag));
    }

}
